﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Taurus.Platform.BLL;
using Taurus.Platform.DTO;
using Taurus.Master.BLL;
using Taurus.Master.DTO;

namespace Taurus.Master
{
    public partial class MasterUI : UserControl
    {
        private AccountBll _account = new AccountBll();

        public MasterUI()
        {
            InitializeComponent();
            dgvFriends.AutoGenerateColumns = false;
        }

        private void MasterUI_Load(object sender, EventArgs e)
        {
            cbxAccount.DataSource = _account.GetUserAccount();
            cbxAccount.DisplayMember = "Name";
        }

        private void cbxAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            UserAccount ua = (UserAccount)cbxAccount.SelectedItem;
            BLL.Master master = InitSys.Instance.GetMaster(ua.UID);
            lblmsg.Text = master.StatusMessage;
            lblLevel.Text = master.Level;
            lblWinning.Text = master.Winning;
            dgvFriends.DataSource = master.Friends;
        }

        private void dgvFriends_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.DesiredType == typeof(Image))
            {
                UserAccount ua = (UserAccount)cbxAccount.SelectedItem;
                BLL.Master master = InitSys.Instance.GetMaster(ua.UID);
                DataGridViewRow row = dgvFriends.Rows[e.RowIndex];
                PKer p = (PKer)row.DataBoundItem;
                switch (master.GetTypeEnum(p))
                {
                    case TypeEnum.Teacher:
                        e.Value = global::Taurus.Master.Properties.Resources.icon_teacher1;
                        break;
                    case TypeEnum.Apprentice:
                        e.Value = global::Taurus.Master.Properties.Resources.icon_disciple;
                        break;
                    case TypeEnum.Empty:
                        e.Value = global::Taurus.Master.Properties.Resources.icon_blank;
                        break;
                    default:
                        break;
                }
            }
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            UserAccount ua = (UserAccount)cbxAccount.SelectedItem;
            BLL.Master master = InitSys.Instance.GetMaster(ua.UID);
            backgroundWorkerPK.RunWorkerAsync(master);
        }

        private void backgroundWorkerPK_DoWork(object sender, DoWorkEventArgs e)
        {
            BLL.Master master = (BLL.Master)e.Argument;
            master.PK();
        }

       
    }
}
